const {ccclass, property} = cc._decorator;


/**电量延迟显示的控制脚本 */
@ccclass
export default class Status extends cc.Component {

    /**电量延迟的总容器 */
    status: cc.Node = null;

    /**cc的红色 */
    red: cc.Color = null;

    /**cc的绿色 */
    green: cc.Color = null;

    /**cc的黄色 */
    yellow: cc.Color = null;

    /**延迟显示的节点 */
    delay: cc.Node = null;

    /**延迟的文字节点 */
    delayLabel: cc.Label = null;

    /**电量的节点 */
    power: cc.Node = null;

    start() {
        this.status = cc.find('Canvas/status');
        this.delay = this.status.getChildByName('delay');
        this.delayLabel = this.delay.getComponent(cc.Label);
        this.power = this.status.getChildByName('power');
        this.red = new cc.Color(205, 0, 0);
        this.green = new cc.Color(0, 205, 0);
        this.yellow = new cc.Color(255, 200, 0);
    }


    update(dt: number){
        // 更新延迟的显示
        const delayMS = cc.vv.net.delayMS;
        if(delayMS != null){
            this.delayLabel.string = delayMS + 'ms';
            if(delayMS > 800){
                this.delay.color = this.red;
            }
            else if(delayMS > 300){
                this.delay.color = this.yellow;
            }
            else{
                this.delay.color = this.green;
            }
        }
        else{
            this.delayLabel.string = 'N/A';
            this.delay.color = this.red;
        }

        // 更新电量的显示
        if(!cc.vv.debug){
            this.power.scaleX = cc.vv.anysdkMgr.getBatteryPercent();
        }
    }

    
}
